package com.tingyuge.test.lazy;

import com.tingyuge.entity.Order;
import com.tingyuge.entity.User;
import com.tingyuge.mapper.IOrderMapper;
import com.tingyuge.mapper.IUserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/6/6
 */
public class MybatisTest1 {

    private IUserMapper userMapper;
    private IOrderMapper orderMapper;
    private SqlSession sqlSession;
    private  SqlSessionFactory sqlSessionFactory;
    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
         sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
         sqlSession = sqlSessionFactory.openSession(true);
         userMapper = sqlSession.getMapper(IUserMapper.class);
        orderMapper = sqlSession.getMapper(IOrderMapper.class);
    }

    @Test
    public void testCacheLeve1(){
        List<User> all = userMapper.findAll();

        for (User user : all) {
            System.out.println(user.getId());
        }
    }  @Test
    public void testOrderLazy(){
        List<Order> all = orderMapper.findAll();
        for (Order order : all) {
            System.out.println(order.getUser());
        }
    }


}
